/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.SourceFile;

import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import javax.swing.JOptionPane;

/**
 *
 * @author 望舒凌霄
 */
public class CMYKAndRGB {
    ICC_Profile ICC_pf;
    ICC_ColorSpace ICC_ClSpace;
  //以下变量存储CMYK颜色值，取值为0到100
    int C = 9;
    int M = 9;
    int Y = 9;
    int K = 9;
    public CMYKAndRGB() {
        String Filename = "USWebCoatedSWOP.icc";
        GetICCFrompfFile(Filename);
    }

    void GetICCFrompfFile(String Filename) {
        try {
          //ICC_pf = ICC_Profile.getInstance(Filename);
          ICC_ClSpace=new ICC_ColorSpace(ICC_Profile.getInstance(ICC_Profile.class.getClassLoader().getResourceAsStream("USWebCoatedSWOP.icc")));
        }
        catch (Exception e) {
          JOptionPane.showMessageDialog(null, "Can''t create ICC_Profile");
        }
        ICC_ClSpace = new ICC_ColorSpace(ICC_pf);
        ColorSpace CS=ColorSpace.getInstance(ColorSpace.TYPE_CMYK);
//        ICC_ColorSpace ICS=new ICC_ColorSpace();
//        ICC_ClSpace=CS;
    }

  //由RGB色彩空间变换到CMYK
    public float[] RGBtoCMYK(Color RGBColor) {
        float[] CMYKfloat = ICC_ClSpace.fromRGB
            (RGBColor.getRGBComponents(null));
        C = (int) (CMYKfloat[0] * 100);
        M = (int) (CMYKfloat[1] * 100);
        Y = (int) (CMYKfloat[2] * 100);
        K = (int) (CMYKfloat[3] * 100);
        return CMYKfloat;
    }

//由CMYK色彩空间变换到RGB
    public Color CMYKtoRGB(float[] CMYKfloat) {
        Color RGBColor = new Color(ICC_ClSpace,
                                   CMYKfloat, 1.0f);
        return RGBColor;
    }

    public Color CMYKtoRGB() {
        float[] CMYKfloat = new float[4];
        CMYKfloat[0] = 0.01f * (float) C;
        CMYKfloat[1] = 0.01f * (float) M;
        CMYKfloat[2] = 0.01f * (float) Y;
        CMYKfloat[3] = 0.01f * (float) K;
        Color RGBColor = new Color(ICC_ClSpace, CMYKfloat, 1.0f);
        return RGBColor;
    }
}
